// pages/map/map.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    subKey: 'PUGBZ-OYQC7-3C2XZ-PNP3X-CLDJQ-ROFOT',
    enable3d: false,
    showLocation: true,
    showCompass: false,
    enableOverlooking: false,
    enableZoom: true,
    enableScroll: true,
    enableRotate: false,
    drawPolygon: false,
    enableSatellite: false,
    enableTraffic: false,
    latitude: 24.374793, // 厦门大学嘉庚学院的纬度
    longitude: 118.048373, // 厦门大学嘉庚学院的经度
    markers: [],
    circles: [],
    polylines: [],
    polygons: [],
    showDialog: false,
    currentMarker: null,
    features: [
      {"name": "嘉庚学院主楼", "geometry": {"type": "Point", "coordinates": [118.100846, 24.445636]}},
      {"name": "嘉庚学院图书馆", "geometry": {"type": "Point", "coordinates": [118.101234, 24.446372]}},
      {"name":"宿舍楼","geometry":{"type":"Point","coordinates":[120.132107,30.874020]}},
      {"name":"食堂","geometry":{"type":"Point","coordinates":[120.135220,30.874841]}},
      {"name":"图书馆","geometry":{"type":"Point","coordinates":[120.129169,30.872750]}}
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    const map = wx.createMapContext("map");
    // map.moveToLocation();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const markers = this.data.features.map((feature,index) => {
      return {
        id: index,
        latitude: feature.geometry.coordinates[1],
        longitude: feature.geometry.coordinates[0],
        properties: {
          name: feature.name,
          code: feature.code
        },
        width: 24,
        height: 24,
        iconPath: '../../images/location.png'
      }
    });
    
    this.setData({
      markers: markers
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  handleMarkerTap(e) {
    console.log(e);
    const marker = this.data.markers.find(item => item.id == e.markerId);
    marker && this.setData({
      currentMarker: marker,
      showDialog: true
    });
    //this.data.showDialog = true;
  },

  navi1() {
    const latitude = this.data.currentMarker.latitude;
    const longitude = this.data.currentMarker.longitude;
    wx.openLocation({
      latitude,
      longitude,
      scale: 18
    });
  },

  navi2() {
    let plugin = requirePlugin('routePlan');
    let key = '7XHBZ-OC7CW-DVNRE-RJOMF-QSD23-JYBA4';  //使用在腾讯位置服务申请的key
    let referer = 'wx4f23dd0ee526ad45';   //调用插件的app的名称
    let endPoint = JSON.stringify({  //终点
        'name': this.data.currentMarker.properties['name'],
        'latitude': this.data.currentMarker.latitude,
        'longitude': this.data.currentMarker.longitude
    });
    wx.navigateTo({
        url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint
    });
  }
})